package com.umeox.watch.moto.dataservice.receiver;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.umeox.watch.moto.dataservice.DataProvider;
import com.umeox.watch.moto.dataservice.constants.Constants;
import com.umeox.watch.moto.dataservice.mqtt.impl.MqttServiceDelegate;
import com.umeox.watch.moto.dataservice.mqtt.receiver.PingReceiver;
import com.umeox.watch.moto.dataservice.services.LocationService;


public class ShutdownReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(final Context context, final Intent intent) {
        String action = intent.getAction();
        if (Intent.ACTION_SHUTDOWN.equals(action)) {
            //关闭闹钟
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                    new Intent(PingReceiver.PING_RECEIVED_INTENT),
                    PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager aMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            aMgr.cancel(pendingIntent);
            //停止服务
            MqttServiceDelegate.stopService(context);
        }else if(Constants.ACTION_POWEROFF.equals(action)){
            //定位
            if (DataProvider.isActivated() && DataProvider.isReportShutdownLocation()) {
                LocationService.shutdownLocation(context);
            }
            DataProvider.resetFlag();
        }
    }
}
